//
//  RNOLResetPayPasswordView.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/18.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLResetPayPasswordView.h"
#import "RNOLNumPadKeyboardView.h"

@interface RNOLResetPayPasswordView()<RNOLNumPadKeyboardProtocol,UITextFieldDelegate>

@property (nonatomic,strong) RNOLNumPadKeyboardView *oneInputView;
@property (nonatomic,strong) RNOLNumPadKeyboardView *twoInputView;

@end

@implementation RNOLResetPayPasswordView

- (RNOLNumPadKeyboardView *)oneInputView{
    if (!_oneInputView) {
        _oneInputView = [RNOLNumPadKeyboardView FromXIB];
        _oneInputView.delegate = self;
    }
    return _oneInputView;
}

- (RNOLNumPadKeyboardView *)twoInputView{
    if (!_twoInputView) {
        _twoInputView = [RNOLNumPadKeyboardView FromXIB];
        _twoInputView.delegate = self;
    }
    return _twoInputView;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.onePasswordTextField.inputView =  self.oneInputView;
    self.twoPasswordTextField.inputView =  self.twoInputView;
}

- (IBAction)competedPayPassWordAction:(UIButton *)sender {
    [self commitAction];
}

#pragma mark- 提交
- (void)commitAction{
    NSString*onePassword = self.onePasswordTextField.text;
    NSString*twoPassword = self.twoPasswordTextField.text;
    if (onePassword.length == 0 || twoPassword.length == 0) {
        [kRootVC showErrorMessageWithText:@"密码不可为空"];
        return ;
    }
    if (onePassword.length != 6) {
        [kRootVC showErrorMessageWithText:@"密码位数不对,6位数字密码请确认!"];
        return ;
    }
    
    if (!kCompareTwoString(onePassword, twoPassword)) {
        [kRootVC showErrorMessageWithText:@"两次输入密码不一致,请重新输入设置!"];
        return;
    }
    [self resignKeyboard];
    !self.competedBlock? : self.competedBlock();
}
#pragma mark- 退键盘
- (void)resignKeyboard{
    if ([self.onePasswordTextField isFirstResponder]) {
        [self.onePasswordTextField resignFirstResponder];
    }
    if ([self.twoPasswordTextField isFirstResponder]) {
        [self.twoPasswordTextField resignFirstResponder];
    }
}
#pragma mark- <RNOLNumPadKeyboardProtocol>
-(void)rnol_keyboardInputDidEnd:(NSString *)text{
    if ([self.onePasswordTextField isFirstResponder]) {
        self.onePasswordTextField.text = self.oneInputView.inputText;
    }
    if ([self.twoPasswordTextField isFirstResponder]) {
        self.twoPasswordTextField.text = self.twoInputView.inputText;
    }
}

- (void)rnol_keyboardInputComeplete{
    //完成
    [self resignKeyboard];
}

- (void)rnol_keyboardInputDeleteBackSpace{
    if ([self.onePasswordTextField isFirstResponder]) {
        if (self.oneInputView.inputText.length <= 0) {
            return;
        }
        [self.oneInputView.inputText deleteCharactersInRange:NSMakeRange([self.oneInputView.inputText length] - 1, 1)];
        self.onePasswordTextField.text =  self.oneInputView.inputText;
    }
    
    if ([self.twoPasswordTextField isFirstResponder]) {
        if (self.twoInputView.inputText.length <= 0) {
            return;
        }
        [self.twoInputView.inputText deleteCharactersInRange:NSMakeRange([self.twoInputView.inputText length] - 1, 1)];
        self.twoPasswordTextField.text = self.twoInputView.inputText;
    }
}

#pragma mark- <UITextFieldDelegate>
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    //监听clear button清空事件
    if ([self.onePasswordTextField isFirstResponder]) {
        self.oneInputView.inputText = nil;
    }
    if ([self.twoPasswordTextField isFirstResponder]) {
        self.twoInputView.inputText = nil;
    }
    return YES;
}

@end
